package cn.mesmile.activity.listener1;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.event.ActivitiEventDispatcher;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

/**
 * @author zb
 * @date 2021/12/23 15:32
 * @Description  流程全局监听器配置
 */
@Slf4j
@Configuration
public class ActivityListenerConfig implements ApplicationListener<ApplicationEvent> {

    private final SpringProcessEngineConfiguration configuration;

    private final TaskCreateListener taskCreateListener;

    private final TaskCompletedListener taskCompletedListener;

    private final ActivityEventGlobalListener globalListener;

    private final ProcessStartListener processStartListener;

    public ActivityListenerConfig(SpringProcessEngineConfiguration configuration, TaskCreateListener taskCreateListener, TaskCompletedListener taskCompletedListener, ActivityEventGlobalListener globalListener, ProcessStartListener processStartListener) {
        this.configuration = configuration;
        this.taskCreateListener = taskCreateListener;
        this.taskCompletedListener = taskCompletedListener;
        this.globalListener = globalListener;
        this.processStartListener = processStartListener;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        // 这里添加需要监听的事件
        ActivitiEventDispatcher eventDispatcher = configuration.getEventDispatcher();
        // 监听任务开始
        eventDispatcher.addEventListener(taskCreateListener, ActivitiEventType.TASK_CREATED);
        // 监听任务结束
        eventDispatcher.addEventListener(taskCompletedListener, ActivitiEventType.TASK_COMPLETED);
        // 监听任务结束
//        eventDispatcher.addEventListener(taskCompletedListener, ActivitiEventType.TASK_COMPLETED);
        // 监听流程创建
        eventDispatcher.addEventListener(processStartListener,ActivitiEventType.PROCESS_STARTED);
        // 添加全局监听
        eventDispatcher.addEventListener(globalListener);
    }

    /**

     <bean id="globalEventListener" class="GlobalEventListener">
        <property name="handlers">
            <map>
                <entry key="TASK_CREATED">
                    <bean class="TaskCreateListener"/>
                entry>
                <entry key="TASK_COMPLETED">
                    <bean class="TaskCompleteListener"/>
                entry>
            map>
        property>
     bean>
     */
}
